$(document).ready(function() {
    var app_key = "AnMcbe_Gi9msizyM9c4tMB8iO3-RqAsP";
    var api_secret = "hp4XU50OsNFl26kYbOpKJGP67BbjqFpK";
    var aVideo, aCanvas, rCanvas, ctx, ctx_r = null;
    var faceset_token = "be892562c0aab896bfc02d8fee6cc544";
    // 识别人脸照片
    function auth_face() {
        var face_image = rCanvas.toDataURL("image/jpeg");
        face_image = face_image.replace('data:image/jpeg;base64,', '');
        $.ajax({
            type: 'POST',
            url: "/search",
            data: {
                face_image: face_image
            },
            dataType: 'JSON',
            success: function(result) {
                if (result.faces.length > 0) {
                    alert(result.faces[0].user_id);
                }
            }
        });
    };


    function init_cam() {
        aVideo = document.getElementById('video');
        aVideo.controls = true;
        aCanvas = document.getElementById('canvas');
        rCanvas = document.getElementById('canvas_r');
        ctx = aCanvas.getContext('2d');
        ctx_r = rCanvas.getContext('2d');
        navigator.getUserMedia = navigator.getUserMedia ||
            navigator.webkitGetUserMedia ||
            navigator.mozGetUserMedia ||
            navigator.msGetUserMedia;
        if (navigator.getUserMedia) {
            navigator.getUserMedia({
                    audio: false,
                    video: {
                        width: window.innerWidth,
                        height: window.innerHeight
                    }
                },
                function(stream) {
                    if ("srcObject" in video) {
                        aVideo.srcObject = stream;
                    } else {
                        // 防止再新的浏览器里使用它，应为它已经不再支持了
                        aVideo.src = window.URL.createObjectURL(stream);
                    }
                    aVideo.onloadedmetadata = function(e) {
                        aVideo.play();
                    };
                },
                function(err) {
                    console.log("获取高拍仪图像引发异常: " + err.name);
                }
            );
        } else {
            console.log("浏览器不支持获得高拍仪数据");
        }
    };

    function cam() {
        ctx.drawImage(aVideo, 0, 0, 400, 300);
        ctx_r.drawImage(aVideo, 0, 0, 400, 300);
    };

    function canvas_to_image() {
        if (!rCanvas || rCanvas == null) {
            return null;
        }
        var data = rCanvas.toDataURL("image/jpeg");
        // dataURL 的格式为 “data:image/jpeg;base64,****”,逗号之前都是一些说明性的文字，我们只需要逗号之后的就行了
        data = data.split(',')[1];
        data = window.atob(data);
        var ia = new Uint8Array(data.length);
        for (var i = 0; i < data.length; i++) {
            ia[i] = data.charCodeAt(i);
        };
        // canvas.toDataURL 返回的默认格式就是 image/jpeg
        var blob = new Blob([ia], { type: "image/jpeg" });
        return blob;
    };

    function init() {
        $(".container").click(function() {
            cam();
            var self = $(this);
            self.animate({
                opacity: 0.25
            }, 300, function() {
                self.animate({
                    opacity: 1
                }, 300, function() {

                });
            });
            auth_face();
        });
        init_cam();
    };
    init();
});